/**
 * $Id: SimpleEntityProvider.java 77 2009-05-31 08:28:40Z azeckoski $
 * $URL: https://entitybus.googlecode.com/svn/trunk/webapp/src/main/java/org/sakaiproject/rest/providers/SimpleEntityProvider.java $
 * SimpleEntityProvider.java - EB - Feb 17, 2009 9:38:32 AM - azeckoski
 **************************************************************************
 * Copyright (c) 2008 Aaron Zeckoski
 * Licensed under the Apache License, Version 2.0
 * 
 * A copy of the Apache License has been included in this 
 * distribution and is available at: http://www.apache.org/licenses/LICENSE-2.0.txt
 *
 * Aaron Zeckoski (azeckoski @ gmail.com) (aaronz @ vt.edu) (aaron @ caret.cam.ac.uk)
 */

package org.sakaiproject.entitybus.providers;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.http.HttpServletResponse;

import org.sakaiproject.entitybus.EntityView;
import org.sakaiproject.entitybus.entityprovider.CoreEntityProvider;
import org.sakaiproject.entitybus.entityprovider.annotations.EntityCustomAction;
import org.sakaiproject.entitybus.entityprovider.capabilities.ActionsExecutable;
import org.sakaiproject.entitybus.entityprovider.capabilities.Describeable;
import org.sakaiproject.entitybus.entityprovider.extension.ActionReturn;


/**
 * This is tests the ability to return binary data
 * 
 * @author Aaron Zeckoski (azeckoski @ gmail.com)
 */
public class BinaryEntityProvider implements CoreEntityProvider, ActionsExecutable, Describeable {

    /**
     * Sample code to demonstrate handling binary data
     * @param os the outputstream to dump the data into
     * @return the AR object with valid status codes and mimetypes
     */
    @EntityCustomAction(action="image", viewKey=EntityView.VIEW_LIST)
    public ActionReturn image(OutputStream os) {
        ActionReturn ar = new ActionReturn(os);
        ar.responseCode = HttpServletResponse.SC_OK;
        ar.mimeType = "image/jpeg";
        ar.encoding = "BASE64";
        ar.headers.put("max-age", "100000");
        // load the image
        InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("image/azeckoski.jpg");
        if (is == null) {
            throw new IllegalStateException("Could not find the image");
        }
        BufferedInputStream bis = new BufferedInputStream(is, 1024);
        try {
            byte[] buffer = new byte[1024];
            while (bis.read(buffer, 0, 1024) > 0) {
                os.write(buffer);
            }
        } catch (IOException e) {
            throw new IllegalStateException("IO failure: " + e, e);
        } finally {
            try {
                bis.close();
            } catch (IOException e) {
                // nothing to do
            }
        }
        // end image load
        return ar;
    }
    
    /* (non-Javadoc)
     * @see org.sakaiproject.entitybus.entityprovider.CoreEntityProvider#entityExists(java.lang.String)
     */
    public boolean entityExists(String id) {
        return true; // default
    }

    /* (non-Javadoc)
     * @see org.sakaiproject.entitybus.entityprovider.EntityProvider#getEntityPrefix()
     */
    public String getEntityPrefix() {
        return "binary";
    }

}
